﻿
Imports ATS.Business.Entities.EntityStateClass

Imports System
Imports System.Collections.Generic
Imports System.Collections.ObjectModel
Imports System.Collections.Specialized
Imports System.ComponentModel
Imports System.Globalization
Imports System.Runtime.Serialization
Imports System.Runtime.CompilerServices

<DataContract(IsReference:=True)>
<KnownType(GetType(Category))>
<KnownType(GetType(Department))>
<KnownType(GetType(DisposedAsset))>
<KnownType(GetType(Image))>
<KnownType(GetType(LeasedAsset))>
<KnownType(GetType(Location))>
<KnownType(GetType(Person))>
<KnownType(GetType(Warranty))>
Public Class MainAssetsTable
    Implements INotifyPropertyChanged, IObjectWithChangeTracker, IEditableObject
    Private _isDirty As Boolean
    Private mainAssetData As MainAssetsTable

#Region "Load Events"
    Public Sub New()
    End Sub


    'Public Sub New(RFIDno As String, _
    '               Assetno As String, _
    '               DateAcquired As Nullable(Of Date), _
    '               AssetAmount As Nullable(Of Decimal), _
    '               CategoryCode As String, _
    '               LocationCode As String, _
    '               DeptCode As String, _
    '               Section As String, _
    '               AssignToEmployeeCode As String, _
    '               DescriptiveName As String, _
    '               CreatedBy As String, _
    '               DateCreated As Nullable(Of Date), _
    '               LastModified As Nullable(Of Date), _
    '               LastModifiedBy As String, _
    '               EstimatedValue As Nullable(Of Decimal), _
    '               ReplacementCost As Nullable(Of Decimal), _
    '               Condition As String, _
    '               Status As String, _
    '               SerialNumber As String, _
    '               InvoiceNumber As String, _
    '               BoughtFrom As String, _
    '               BaseCost As Nullable(Of Decimal), _
    '               VAT As Nullable(Of Decimal), _
    '               Manufacturer As String, _
    '               BrandName As String, _
    '               ModelNumber As String, _
    '               Remarks As String, _
    '               SalvageValue As Nullable(Of Decimal), _
    '               LastScanDate As Nullable(Of Date) _
    '               )

    '    _RFIDno = RFIDno
    '    _Assetno = Assetno
    '    _DateAcquired = DateAcquired
    '    _AssetAmount = AssetAmount
    '    _CategoryCode = CategoryCode
    '    _LocationCode = LocationCode
    '    _DeptCode = DeptCode
    '    _Section = Section
    '    _AssignToEmployeeCode = AssignToEmployeeCode
    '    _DescriptiveName = DescriptiveName
    '    _CreatedBy = CreatedBy
    '    _DateCreated = DateCreated
    '    _LastModified = LastModified
    '    _LastModifiedBy = LastModifiedBy
    '    _EstimatedValue = EstimatedValue
    '    _ReplacementCost = ReplacementCost
    '    _Condition = Condition
    '    _Status = Status
    '    _SerialNumber = SerialNumber
    '    _InvoiceNumber = InvoiceNumber
    '    _BoughtFrom = BoughtFrom
    '    _BaseCost = BaseCost
    '    _VAT = VAT
    '    _Manufacturer = Manufacturer
    '    _BrandName = BrandName
    '    _ModelNumber = ModelNumber
    '    _Remarks = Remarks
    '    _SalvageValue = SalvageValue
    '    _LastScanDate = LastScanDate

    'End Sub
#End Region

#Region "Primitive Properties"

    Public ReadOnly Property IsDirty() As Boolean
        Get
            Return Me.EntityState <> EntityStateEnum.Unchanged
        End Get

    End Property

    <DataMember()>
    Public Property RFIDno() As String
        Get
            Return _rFIDno
        End Get
        Set(ByVal value As String)
            If Not Equals(_rFIDno, value) Then
                If value <> _rFIDno Then
                    Me.DataStateChanged( _
                    EntityStateEnum.Modified, _
                    "RFIDno")
                End If
                _rFIDno = value
                OnPropertyChanged("RFIDno")
            End If
        End Set
    End Property

    Private _rFIDno As String

    <DataMember()>
    Public Property Assetno() As String
        Get
            Return _assetno
        End Get
        Set(ByVal value As String)
            If Not Equals(_assetno, value) Then
                If ChangeTracker.ChangeTrackingEnabled AndAlso ChangeTracker.State <> ObjectState.Added Then
                    Throw New InvalidOperationException("The property 'Assetno' is part of the object's key and cannot be changed. Changes to key properties can only be made when the object is not being tracked or is in the Added state.")
                End If
                If Not value Is Nothing Then
                    If value <> _assetno Then
                        Me.DataStateChanged( _
                        EntityStateEnum.Modified, _
                        "Assetno")
                    End If
                Else
                    Throw New Exception("Please provide value for Asset Number.")
                End If
                _assetno = value
                OnPropertyChanged("Assetno")
            End If
        End Set
    End Property

    Private _assetno As String

    <DataMember()>
    Public Property DateAcquired() As Nullable(Of Date)
        Get
            Return _dateAcquired
        End Get
        Set(ByVal value As Nullable(Of Date))
            If Not Equals(_dateAcquired, value) Then
                If _dateAcquired Is Nothing Then
                    _dateAcquired = #12:00:00 AM#
                End If
                If value <> _dateAcquired Then
                    Me.DataStateChanged( _
                    EntityStateEnum.Modified, _
                    "DateAcquired")
                End If
                _dateAcquired = value
                OnPropertyChanged("DateAcquired")
            End If
        End Set
    End Property

    Private _dateAcquired As Nullable(Of Date)

    <DataMember()>
    Public Property AssetAmount() As Nullable(Of Decimal)
        Get
            Return _assetAmount
        End Get
        Set(ByVal value As Nullable(Of Decimal))
            If Not Equals(_assetAmount, value) Then
                If value <> _assetAmount Then
                    Me.DataStateChanged( _
                    EntityStateEnum.Modified, _
                    "AssetAmount")
                End If
                _assetAmount = value
                OnPropertyChanged("AssetAmount")
            End If
        End Set
    End Property

    Private _assetAmount As Nullable(Of Decimal)

    <DataMember()>
    Public Property CategoryCode() As String
        Get
            Return _categoryCode
        End Get
        Set(ByVal value As String)
            If Not Equals(_categoryCode, value) Then
                ChangeTracker.RecordOriginalValue("CategoryCode", _categoryCode)
                If Not IsDeserializing Then
                    If Category IsNot Nothing AndAlso Not Equals(Category.CategoryCode, value) Then
                        Category = Nothing
                    End If
                End If
                If Not value Is Nothing Then
                    If value <> _categoryCode Then
                        Me.DataStateChanged( _
                        EntityStateEnum.Modified, _
                        "CategoryCode")
                    End If
                Else
                    Throw New Exception("Please provide value for Category Code.")
                End If
                _categoryCode = value
                OnPropertyChanged("CategoryCode")
            End If
        End Set
    End Property
    Private _categoryCode As String

    <DataMember()>
    Public Property DescriptiveName() As String
        Get
            Return _descriptiveName
        End Get
        Set(ByVal value As String)
            If Not Equals(_descriptiveName, value) Then
                If Not value Is Nothing Then
                    If value <> _descriptiveName Then
                        Me.DataStateChanged( _
                        EntityStateEnum.Modified, _
                        "DescriptiveName")
                    End If
                Else
                    Throw New Exception("Please provide value for Description.")
                End If
                _descriptiveName = value
                OnPropertyChanged("DescriptiveName")
            End If
        End Set
    End Property

    Private _descriptiveName As String



    'Private _categoryCode As String
    '<DataMember()>
    'Public Property CategoryCode() As String
    '    Get
    '        Return _categoryCode
    '    End Get

    '    Set(ByVal value As String)
    '        If Not value Is Nothing Then
    '            If value <> _categoryCode Then
    '                Me.DataStateChanged( _
    '                EntityStateEnum.Modified, _
    '                "CategoryCode")
    '            End If
    '        Else
    '            Throw New Exception("Please provide value for Category Code.")
    '        End If
    '        _categoryCode = value
    '        OnPropertyChanged("CategoryCode")
    '    End Set
    'End Property

    'Private _LocationCode As String
    '<DataMember()>
    'Public Property LocationCode() As String
    '    Get
    '        Return _LocationCode
    '    End Get

    '    Set(ByVal value As String)
    '        If value <> _LocationCode Then
    '            Me.DataStateChanged( _
    '            EntityStateEnum.Modified, _
    '            "LocationCode")
    '        End If
    '        _LocationCode = value
    '        OnPropertyChanged("LocationCode")
    '    End Set
    'End Property

    <DataMember()>
    Public Property LocationCode() As String
        Get
            Return _locationCode
        End Get
        Set(ByVal value As String)
            If Not Equals(_locationCode, value) Then
                ChangeTracker.RecordOriginalValue("LocationCode", _locationCode)
                If Not IsDeserializing Then
                    If Location IsNot Nothing AndAlso Not Equals(Location.LocationCode, value) Then
                        Location = Nothing
                    End If
                End If
                If value <> _locationCode Then
                    Me.DataStateChanged( _
                    EntityStateEnum.Modified, _
                    "LocationCode")
                End If
                _locationCode = value
                OnPropertyChanged("LocationCode")
            End If
        End Set
    End Property

    Private _locationCode As String


    Private _DeptCode As String
    <DataMember()>
    Public Property DeptCode() As String
        Get
            Return _DeptCode
        End Get

        Set(ByVal value As String)
            If value <> _DeptCode Then
                Me.DataStateChanged( _
                EntityStateEnum.Modified, _
                "DeptCode")
            End If
            _DeptCode = value
            OnPropertyChanged("DeptCode")
        End Set
    End Property




    '<DataMember()>
    'Public Property DeptCode() As String
    '    Get
    '        Return _deptCode
    '    End Get
    '    Set(ByVal value As String)
    '        If Not Equals(_deptCode, value) Then
    '            ChangeTracker.RecordOriginalValue("DeptCode", _deptCode)
    '            If Not IsDeserializing Then
    '                If Department IsNot Nothing AndAlso Not Equals(Department.DepartmentCode, value) Then
    '                    Department = Nothing
    '                End If
    '            End If
    '            If value <> _deptCode Then
    '                Me.DataStateChanged( _
    '                EntityStateEnum.Modified, _
    '                "DeptCode")
    '            End If
    '            _deptCode = value
    '            OnPropertyChanged("DeptCode")
    '        End If
    '    End Set
    'End Property

    'Private _deptCode As String

    <DataMember()>
    Public Property Section() As String
        Get
            Return _section
        End Get
        Set(ByVal value As String)
            If Not Equals(_section, value) Then
                If value <> _section Then
                    Me.DataStateChanged( _
                    EntityStateEnum.Modified, _
                    "Section")
                End If
                _section = value
                OnPropertyChanged("Section")
            End If
        End Set
    End Property

    Private _section As String

    Private _AssignToEmployeeCode As String
    <DataMember()>
    Public Property AssignToEmployeeCode() As String
        Get
            Return _AssignToEmployeeCode
        End Get

        Set(ByVal value As String)
            If value <> _AssignToEmployeeCode Then
                Me.DataStateChanged( _
                EntityStateEnum.Modified, _
                "AssignToEmployeeCode")
            End If
            _AssignToEmployeeCode = value
            OnPropertyChanged("AssignToEmployeeCode")
        End Set
    End Property
    '<DataMember()>
    'Public Property AssignToEmployeeCode() As String
    '    Get
    '        Return _assignToEmployeeCode
    '    End Get
    '    Set(ByVal value As String)
    '        If Not Equals(_assignToEmployeeCode, value) Then
    '            ChangeTracker.RecordOriginalValue("AssignToEmployeeCode", _assignToEmployeeCode)
    '            If Not IsDeserializing Then
    '                If Person IsNot Nothing AndAlso Not Equals(Person.EmployeeID, value) Then
    '                    Person = Nothing
    '                End If
    '            End If
    '            If value <> _assignToEmployeeCode Then
    '                Me.DataStateChanged( _
    '                EntityStateEnum.Modified, _
    '                "AssignToEmployeeCode")
    '            End If
    '            _assignToEmployeeCode = value
    '            OnPropertyChanged("AssignToEmployeeCode")
    '        End If
    '    End Set
    'End Property

    'Private _assignToEmployeeCode As String


    <DataMember()>
    Public Property Createdby() As String
        Get
            Return _createdby
        End Get
        Set(ByVal value As String)
            If Not Equals(_createdby, value) Then
                If value <> _createdby Then
                    Me.DataStateChanged( _
                    EntityStateEnum.Modified, _
                    "Createdby")
                End If
                _createdby = value
                OnPropertyChanged("Createdby")
            End If
        End Set
    End Property

    Private _createdby As String

    <DataMember()>
    Public Property DateCreated() As Nullable(Of Date)
        Get
            Return _dateCreated
        End Get
        Set(ByVal value As Nullable(Of Date))
            If Not Equals(_dateCreated, value) Then
                If _dateCreated Is Nothing Then
                    _dateCreated = #12:00:00 AM#
                End If
                If value <> _dateCreated Then
                    Me.DataStateChanged( _
                    EntityStateEnum.Modified, _
                    "DateCreated")
                End If
                _dateCreated = value
                OnPropertyChanged("DateCreated")
            End If
        End Set
    End Property

    Private _dateCreated As Nullable(Of Date)

    <DataMember()>
    Public Property LastModified() As Nullable(Of Date)
        Get
            Return _lastModified
        End Get
        Set(ByVal value As Nullable(Of Date))
            If Not Equals(_lastModified, value) Then
                If _lastModified Is Nothing Then
                    _lastModified = #12:00:00 AM#
                End If
                If value <> _lastModified Then
                    Me.DataStateChanged( _
                    EntityStateEnum.Modified, _
                    "LastModified")
                End If
                _lastModified = value
                OnPropertyChanged("LastModified")
            End If
        End Set
    End Property

    Private _lastModified As Nullable(Of Date)

    <DataMember()>
    Public Property LastModifiedBy() As String
        Get
            Return _lastModifiedBy
        End Get
        Set(ByVal value As String)
            If Not Equals(_lastModifiedBy, value) Then
                If value <> _lastModifiedBy Then
                    Me.DataStateChanged( _
                    EntityStateEnum.Modified, _
                    "LastModifiedBy")
                End If
                _lastModifiedBy = value
                OnPropertyChanged("LastModifiedBy")
            End If
        End Set
    End Property

    Private _lastModifiedBy As String

    <DataMember()>
    Public Property EstimatedValue() As Nullable(Of Decimal)
        Get
            Return _estimatedValue
        End Get
        Set(ByVal value As Nullable(Of Decimal))
            If Not Equals(_estimatedValue, value) Then
                If value <> _estimatedValue Then
                    Me.DataStateChanged( _
                    EntityStateEnum.Modified, _
                    "EstimatedValue")
                End If
                _estimatedValue = value
                OnPropertyChanged("EstimatedValue")
            End If
        End Set
    End Property

    Private _estimatedValue As Nullable(Of Decimal)


    <DataMember()>
    Public Property SalvageValue As Nullable(Of Decimal)
        Get
            Return _SalvageValue
        End Get

        Set(ByVal value As Nullable(Of Decimal))
            If value Is Nothing Then
                value = 0
            End If
            If Not Equals(_SalvageValue, value) Then
                If value <> _SalvageValue Then
                    Me.DataStateChanged( _
                    EntityStateEnum.Modified, _
                    "SalvageValue")
                End If
                _SalvageValue = value
                OnPropertyChanged("SalvageValue")
            End If

        End Set
    End Property
    Private _SalvageValue As Nullable(Of Decimal)

    <DataMember()>
    Public Property ReplacementCost() As Nullable(Of Decimal)
        Get
            Return _replacementCost
        End Get

        Set(ByVal value As Nullable(Of Decimal))
            If value Is Nothing Then
                value = 0
            End If
            If Not Equals(_replacementCost, value) Then
                If value <> _replacementCost Then
                    Me.DataStateChanged( _
                    EntityStateEnum.Modified, _
                    "ReplacementCost")
                End If
                _replacementCost = value
                OnPropertyChanged("ReplacementCost")
            End If
        End Set
    End Property

    Private _replacementCost As Nullable(Of Decimal)

    <DataMember()>
    Public Property Condition() As String
        Get
            Return _condition
        End Get
        Set(ByVal value As String)
            If Not Equals(_condition, value) Then
                If value <> _condition Then
                    Me.DataStateChanged( _
                    EntityStateEnum.Modified, _
                    "Condition")
                End If
                _condition = value
                OnPropertyChanged("Condition")
            End If
        End Set
    End Property

    Private _condition As String

    <DataMember()>
    Public Property Status() As String
        Get
            Return _status
        End Get
        Set(ByVal value As String)
            If Not Equals(_status, value) Then
                If value <> _status Then
                    Me.DataStateChanged( _
                    EntityStateEnum.Modified, _
                    "Status")
                End If
                _status = value
                OnPropertyChanged("Status")
            End If
        End Set
    End Property

    Private _status As String

    <DataMember()>
    Public Property SerialNumber() As String
        Get
            Return _serialNumber
        End Get
        Set(ByVal value As String)
            If Not Equals(_serialNumber, value) Then
                If value <> _serialNumber Then
                    Me.DataStateChanged( _
                    EntityStateEnum.Modified, _
                    "SerialNumber")
                End If
                _serialNumber = value
                OnPropertyChanged("SerialNumber")
            End If
        End Set
    End Property

    Private _serialNumber As String

    <DataMember()>
    Public Property InvoiceNumber() As String
        Get
            Return _invoiceNumber
        End Get
        Set(ByVal value As String)
            If Not Equals(_invoiceNumber, value) Then
                If value <> _invoiceNumber Then
                    Me.DataStateChanged( _
                    EntityStateEnum.Modified, _
                    "InvoiceNumber")
                End If
                _invoiceNumber = value
                OnPropertyChanged("InvoiceNumber")
            End If
        End Set
    End Property

    Private _invoiceNumber As String

    <DataMember()>
    Public Property BoughtFrom() As String
        Get
            Return _boughtFrom
        End Get
        Set(ByVal value As String)
            If Not Equals(_boughtFrom, value) Then
                If value <> _boughtFrom Then
                    Me.DataStateChanged( _
                    EntityStateEnum.Modified, _
                    "BoughtFrom")
                End If
                _boughtFrom = value
                OnPropertyChanged("BoughtFrom")
            End If
        End Set
    End Property

    Private _boughtFrom As String

    <DataMember()>
    Public Property BaseCost() As Nullable(Of Decimal)
        Get
            Return _baseCost
        End Get
        Set(ByVal value As Nullable(Of Decimal))
            If Not Equals(_baseCost, value) Then
                If value <> _baseCost Then
                    Me.DataStateChanged( _
                    EntityStateEnum.Modified, _
                    "BaseCost")
                End If
                _baseCost = value
                OnPropertyChanged("BaseCost")
                OnPropertyChanged("TotalCost")
            End If
        End Set
    End Property

    Private _baseCost As Nullable(Of Decimal)

    <DataMember()>
    Public Property VAT() As Nullable(Of Decimal)
        Get
            Return _vAT
        End Get
        Set(ByVal value As Nullable(Of Decimal))
            If Not Equals(_vAT, value) Then
                If value <> _vAT Then
                    Me.DataStateChanged( _
                    EntityStateEnum.Modified, _
                    "VAT")
                End If
                _vAT = value
                OnPropertyChanged("VAT")
                OnPropertyChanged("TotalCost")
            End If
        End Set
    End Property

    Private _vAT As Nullable(Of Decimal)


    Private _TotalCost As Nullable(Of Decimal)
    Public ReadOnly Property TotalCost() As Nullable(Of Decimal)
        Get
            Return If(BaseCost Is Nothing, 0, BaseCost) + If(VAT Is Nothing, 0, VAT)
        End Get

        'Set(value As Nullable(Of Decimal))
        '    _TotalCost = value
        'End Set
    End Property



    <DataMember()>
    Public Property Manufacturer() As String
        Get
            Return _manufacturer
        End Get
        Set(ByVal value As String)
            If Not Equals(_manufacturer, value) Then
                If value <> _manufacturer Then
                    Me.DataStateChanged( _
                    EntityStateEnum.Modified, _
                    "Manufacturer")
                End If
                _manufacturer = value
                OnPropertyChanged("Manufacturer")
            End If
        End Set
    End Property

    Private _manufacturer As String

    <DataMember()>
    Public Property BrandName() As String
        Get
            Return _brandName
        End Get
        Set(ByVal value As String)
            If Not Equals(_brandName, value) Then
                If value <> _brandName Then
                    Me.DataStateChanged( _
                    EntityStateEnum.Modified, _
                    "BrandName")
                End If
                _brandName = value
                OnPropertyChanged("BrandName")
            End If
        End Set
    End Property

    Private _brandName As String

    <DataMember()>
    Public Property ModelNumber() As String
        Get
            Return _modelNumber
        End Get
        Set(ByVal value As String)
            If Not Equals(_modelNumber, value) Then
                If value <> _modelNumber Then
                    Me.DataStateChanged( _
                    EntityStateEnum.Modified, _
                    "ModelNumber")
                End If
                _modelNumber = value
                OnPropertyChanged("ModelNumber")
            End If
        End Set
    End Property

    Private _modelNumber As String

    <DataMember()>
    Public Property Remarks() As String
        Get
            Return _remarks
        End Get
        Set(ByVal value As String)
            If Not Equals(_remarks, value) Then
                If value <> _remarks Then
                    Me.DataStateChanged( _
                    EntityStateEnum.Modified, _
                    "Remarks")
                End If
                _remarks = value
                OnPropertyChanged("Remarks")
            End If
        End Set
    End Property

    Private _remarks As String





    <DataMember()>
    Public Property LastScanDate() As Nullable(Of Date)
        Get
            Return _lastScanDate
        End Get
        Set(ByVal value As Nullable(Of Date))
            If Not Equals(_lastScanDate, value) Then
                If _lastScanDate Is Nothing Then
                    _lastScanDate = #12:00:00 AM#
                End If
                If value <> _lastScanDate Then
                    Me.DataStateChanged( _
                    EntityStateEnum.Modified, _
                    "LastScanDate")
                End If
                _lastScanDate = value
                OnPropertyChanged("LastScanDate")
            End If
        End Set
    End Property

    Private _lastScanDate As Nullable(Of Date)

#End Region
#Region "Navigation Properties"

    <DataMember()>
    Public Property Category() As Category
        Get
            Return _category
        End Get
        Set(ByVal value As Category)
            If _category IsNot value Then
                Dim previousValue As Category = _category
                _category = value
                FixupCategory(previousValue)
                OnNavigationPropertyChanged("Category")
            End If
        End Set
    End Property

    Private _category As Category


    <DataMember()>
    Public Property Department() As Department
        Get
            Return _department
        End Get
        Set(ByVal value As Department)
            If _department IsNot value Then
                Dim previousValue As Department = _department
                _department = value
                FixupDepartment(previousValue)
                OnNavigationPropertyChanged("Department")
            End If
        End Set
    End Property

    Private _department As Department


    <DataMember()>
    Public Property DisposedAsset() As DisposedAsset
        Get
            Return _disposedAsset
        End Get
        Set(ByVal value As DisposedAsset)
            If _disposedAsset IsNot value Then
                Dim previousValue As DisposedAsset = _disposedAsset
                Me.DataStateChanged( _
               EntityStateEnum.Modified, _
               "DisposedAsset")
                _disposedAsset = value
                FixupDisposedAsset(previousValue)
                OnNavigationPropertyChanged("DisposedAsset")
            End If
        End Set
    End Property

    Private _disposedAsset As DisposedAsset


    <DataMember()>
    Public Property Images() As TrackableCollection(Of Image)
        Get
            If _images Is Nothing Then
                _images = New TrackableCollection(Of Image)
                AddHandler _images.CollectionChanged, AddressOf FixupImages
            End If
            Return _images
        End Get
        Set(ByVal value As TrackableCollection(Of Image))
            If _images IsNot value Then
                If ChangeTracker.ChangeTrackingEnabled Then
                    Throw New InvalidOperationException("Cannot set the FixupChangeTrackingCollection when ChangeTracking is enabled")
                End If
                If _images IsNot Nothing Then
                    RemoveHandler _images.CollectionChanged, AddressOf FixupImages
                End If
                _images = value
                If _images IsNot Nothing Then
                    AddHandler _images.CollectionChanged, AddressOf FixupImages
                End If
                OnNavigationPropertyChanged("Images")
            End If
        End Set
    End Property

    Private _images As TrackableCollection(Of Image)

    <DataMember()>
    Public Property LeasedAsset() As LeasedAsset
        Get
            Return _leasedAsset
        End Get
        Set(ByVal value As LeasedAsset)
            If _leasedAsset IsNot value Then
                Dim previousValue As LeasedAsset = _leasedAsset
                _leasedAsset = value
                FixupLeasedAsset(previousValue)
                OnNavigationPropertyChanged("LeasedAsset")
            End If
        End Set
    End Property

    Private _leasedAsset As LeasedAsset


    <DataMember()>
    Public Property Location() As Location
        Get
            Return _location
        End Get
        Set(ByVal value As Location)
            If _location IsNot value Then
                Dim previousValue As Location = _location
                _location = value
                FixupLocation(previousValue)
                OnNavigationPropertyChanged("Location")
            End If
        End Set
    End Property

    Private _location As Location


    <DataMember()>
    Public Property Person() As Person
        Get
            Return _person
        End Get
        Set(ByVal value As Person)
            If _person IsNot value Then
                Dim previousValue As Person = _person
                _person = value
                FixupPerson(previousValue)
                OnNavigationPropertyChanged("Person")
            End If
        End Set
    End Property

    Private _person As Person


    <DataMember()>
    Public Property Warranty() As Warranty
        Get
            Return _warranty
        End Get
        Set(ByVal value As Warranty)
            If _warranty IsNot value Then
                Dim previousValue As Warranty = _warranty
                _warranty = value
                FixupWarranty(previousValue)
                OnNavigationPropertyChanged("Warranty")
            End If
        End Set
    End Property

    Private _warranty As Warranty


#End Region
#Region "ChangeTracking"

    Protected Overridable Sub OnPropertyChanged(ByVal propertyName As String)
        If ChangeTracker.State <> ObjectState.Added AndAlso ChangeTracker.State <> ObjectState.Deleted Then
            ChangeTracker.State = ObjectState.Modified
        End If
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
    End Sub

    Protected Overridable Sub OnNavigationPropertyChanged(ByVal propertyName As String)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
    End Sub

    Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
    Private _changeTracker As ObjectChangeTracker

    <DataMember()>
    Public Property ChangeTracker() As ObjectChangeTracker Implements IObjectWithChangeTracker.ChangeTracker
        Get
            If _changeTracker Is Nothing Then
                _changeTracker = New ObjectChangeTracker()
                AddHandler _changeTracker.ObjectStateChanging, AddressOf HandleObjectStateChanging
            End If
            Return _changeTracker
        End Get
        Set(ByVal value As ObjectChangeTracker)
            If _changeTracker IsNot Nothing Then
                RemoveHandler _changeTracker.ObjectStateChanging, AddressOf HandleObjectStateChanging
            End If
            _changeTracker = value
            If _changeTracker IsNot Nothing Then
                AddHandler _changeTracker.ObjectStateChanging, AddressOf HandleObjectStateChanging
            End If
        End Set
    End Property

    Private Sub HandleObjectStateChanging(ByVal sender As Object, ByVal e As ObjectStateChangingEventArgs)
        If e.NewState = ObjectState.Deleted Then
            Me.ClearNavigationProperties()
        End If
    End Sub

    Private _isDeserializing As Boolean
    Protected Property IsDeserializing() As Boolean
        Get
            Return _isDeserializing
        End Get
        Private Set(ByVal value As Boolean)
            _isDeserializing = value
        End Set
    End Property

    <OnDeserializing()>
    Public Sub OnDeserializingMethod(ByVal context As StreamingContext)
        IsDeserializing = True
    End Sub

    <OnDeserialized()>
    Public Sub OnDeserializedMethod(ByVal context As StreamingContext)
        IsDeserializing = False
        ChangeTracker.ChangeTrackingEnabled = True
    End Sub

    Protected Overridable Sub ClearNavigationProperties()
        Category = Nothing
        Department = Nothing
        DisposedAsset = Nothing
        Images.Clear()
        LeasedAsset = Nothing
        Location = Nothing
        Person = Nothing
        Warranty = Nothing
    End Sub

#End Region
#Region "Association Fixup"

    Private Sub FixupCategory(ByVal previousValue As Category, Optional ByVal skipKeys As Boolean = False)
        If IsDeserializing Then
            Return
        End If

        If previousValue IsNot Nothing AndAlso previousValue.MainAssetsTables.Contains(Me) Then
            previousValue.MainAssetsTables.Remove(Me)
        End If

        If Category IsNot Nothing Then
            If Not Category.MainAssetsTables.Contains(Me) Then
                Category.MainAssetsTables.Add(Me)
            End If

            CategoryCode = Category.CategoryCode
        ElseIf Not skipKeys Then
            CategoryCode = Nothing
        End If
        If ChangeTracker.ChangeTrackingEnabled Then
            If ChangeTracker.OriginalValues.ContainsKey("Category") AndAlso
                ChangeTracker.OriginalValues("Category") Is Category Then
                ChangeTracker.OriginalValues.Remove("Category")
            Else
                ChangeTracker.RecordOriginalValue("Category", previousValue)
            End If
            If Category IsNot Nothing AndAlso Not Category.ChangeTracker.ChangeTrackingEnabled Then
                Category.StartTracking()
            End If
        End If
    End Sub

    Private Sub FixupDepartment(ByVal previousValue As Department, Optional ByVal skipKeys As Boolean = False)
        If IsDeserializing Then
            Return
        End If

        If previousValue IsNot Nothing AndAlso previousValue.MainAssetsTables.Contains(Me) Then
            previousValue.MainAssetsTables.Remove(Me)
        End If

        If Department IsNot Nothing Then
            If Not Department.MainAssetsTables.Contains(Me) Then
                Department.MainAssetsTables.Add(Me)
            End If

            DeptCode = Department.DepartmentCode
        ElseIf Not skipKeys Then
            DeptCode = Nothing
        End If
        If ChangeTracker.ChangeTrackingEnabled Then
            If ChangeTracker.OriginalValues.ContainsKey("Department") AndAlso
                ChangeTracker.OriginalValues("Department") Is Department Then
                ChangeTracker.OriginalValues.Remove("Department")
            Else
                ChangeTracker.RecordOriginalValue("Department", previousValue)
            End If
            If Department IsNot Nothing AndAlso Not Department.ChangeTracker.ChangeTrackingEnabled Then
                Department.StartTracking()
            End If
        End If
    End Sub

    Private Sub FixupDisposedAsset(ByVal previousValue As DisposedAsset)
        If previousValue IsNot Nothing Then
            RemoveHandler ChangeTracker.ObjectStateChanging, AddressOf previousValue.HandleCascadeDelete
        End If

        If DisposedAsset IsNot Nothing Then
            AddHandler ChangeTracker.ObjectStateChanging, AddressOf DisposedAsset.HandleCascadeDelete
        End If
        If IsDeserializing Then
            Return
        End If

        If previousValue IsNot Nothing AndAlso previousValue.MainAssetsTable Is Me Then
            previousValue.MainAssetsTable = Nothing
        End If

        If DisposedAsset IsNot Nothing Then
            DisposedAsset.MainAssetsTable = Me
        End If

        If ChangeTracker.ChangeTrackingEnabled Then
            If ChangeTracker.OriginalValues.ContainsKey("DisposedAsset") AndAlso
                ChangeTracker.OriginalValues("DisposedAsset") Is DisposedAsset Then
                ChangeTracker.OriginalValues.Remove("DisposedAsset")
            Else
                ChangeTracker.RecordOriginalValue("DisposedAsset", previousValue)
                ' Delete the dependent end of this identifying association. If the current state is Added,
                ' allow the relationship to be changed without causing the dependent to be deleted.
                If previousValue IsNot Nothing AndAlso previousValue.ChangeTracker.State <> ObjectState.Added Then
                    previousValue.MarkAsDeleted()
                End If
            End If
            If DisposedAsset IsNot Nothing AndAlso Not DisposedAsset.ChangeTracker.ChangeTrackingEnabled Then
                DisposedAsset.StartTracking()
            End If
        End If
    End Sub

    Private Sub FixupLeasedAsset(ByVal previousValue As LeasedAsset)
        If previousValue IsNot Nothing Then
            RemoveHandler ChangeTracker.ObjectStateChanging, AddressOf previousValue.HandleCascadeDelete
        End If

        If LeasedAsset IsNot Nothing Then
            AddHandler ChangeTracker.ObjectStateChanging, AddressOf LeasedAsset.HandleCascadeDelete
        End If
        If IsDeserializing Then
            Return
        End If

        If previousValue IsNot Nothing AndAlso previousValue.MainAssetsTable Is Me Then
            previousValue.MainAssetsTable = Nothing
        End If

        If LeasedAsset IsNot Nothing Then
            LeasedAsset.MainAssetsTable = Me
        End If

        If ChangeTracker.ChangeTrackingEnabled Then
            If ChangeTracker.OriginalValues.ContainsKey("LeasedAsset") AndAlso
                ChangeTracker.OriginalValues("LeasedAsset") Is LeasedAsset Then
                ChangeTracker.OriginalValues.Remove("LeasedAsset")
            Else
                ChangeTracker.RecordOriginalValue("LeasedAsset", previousValue)
                ' Delete the dependent end of this identifying association. If the current state is Added,
                ' allow the relationship to be changed without causing the dependent to be deleted.
                If previousValue IsNot Nothing AndAlso previousValue.ChangeTracker.State <> ObjectState.Added Then
                    previousValue.MarkAsDeleted()
                End If
            End If
            If LeasedAsset IsNot Nothing AndAlso Not LeasedAsset.ChangeTracker.ChangeTrackingEnabled Then
                LeasedAsset.StartTracking()
            End If
        End If
    End Sub

    Private Sub FixupLocation(ByVal previousValue As Location, Optional ByVal skipKeys As Boolean = False)
        If IsDeserializing Then
            Return
        End If

        If previousValue IsNot Nothing AndAlso previousValue.MainAssetsTables.Contains(Me) Then
            previousValue.MainAssetsTables.Remove(Me)
        End If

        If Location IsNot Nothing Then
            If Not Location.MainAssetsTables.Contains(Me) Then
                Location.MainAssetsTables.Add(Me)
            End If

            LocationCode = Location.LocationCode
        ElseIf Not skipKeys Then
            LocationCode = Nothing
        End If
        If ChangeTracker.ChangeTrackingEnabled Then
            If ChangeTracker.OriginalValues.ContainsKey("Location") AndAlso
                ChangeTracker.OriginalValues("Location") Is Location Then
                ChangeTracker.OriginalValues.Remove("Location")
            Else
                ChangeTracker.RecordOriginalValue("Location", previousValue)
            End If
            If Location IsNot Nothing AndAlso Not Location.ChangeTracker.ChangeTrackingEnabled Then
                Location.StartTracking()
            End If
        End If
    End Sub

    Private Sub FixupPerson(ByVal previousValue As Person, Optional ByVal skipKeys As Boolean = False)
        If IsDeserializing Then
            Return
        End If

        If previousValue IsNot Nothing AndAlso previousValue.MainAssetsTables.Contains(Me) Then
            previousValue.MainAssetsTables.Remove(Me)
        End If

        If Person IsNot Nothing Then
            If Not Person.MainAssetsTables.Contains(Me) Then
                Person.MainAssetsTables.Add(Me)
            End If

            AssignToEmployeeCode = Person.EmployeeID
        ElseIf Not skipKeys Then
            AssignToEmployeeCode = Nothing
        End If
        If ChangeTracker.ChangeTrackingEnabled Then
            If ChangeTracker.OriginalValues.ContainsKey("Person") AndAlso
                ChangeTracker.OriginalValues("Person") Is Person Then
                ChangeTracker.OriginalValues.Remove("Person")
            Else
                ChangeTracker.RecordOriginalValue("Person", previousValue)
            End If
            If Person IsNot Nothing AndAlso Not Person.ChangeTracker.ChangeTrackingEnabled Then
                Person.StartTracking()
            End If
        End If
    End Sub

    Private Sub FixupWarranty(ByVal previousValue As Warranty)
        If previousValue IsNot Nothing Then
            RemoveHandler ChangeTracker.ObjectStateChanging, AddressOf previousValue.HandleCascadeDelete
        End If

        If Warranty IsNot Nothing Then
            AddHandler ChangeTracker.ObjectStateChanging, AddressOf Warranty.HandleCascadeDelete
        End If
        If IsDeserializing Then
            Return
        End If

        If previousValue IsNot Nothing AndAlso previousValue.MainAssetsTable Is Me Then
            previousValue.MainAssetsTable = Nothing
        End If

        If Warranty IsNot Nothing Then
            Warranty.MainAssetsTable = Me
        End If

        If ChangeTracker.ChangeTrackingEnabled Then
            If ChangeTracker.OriginalValues.ContainsKey("Warranty") AndAlso
                ChangeTracker.OriginalValues("Warranty") Is Warranty Then
                ChangeTracker.OriginalValues.Remove("Warranty")
            Else
                ChangeTracker.RecordOriginalValue("Warranty", previousValue)
                ' Delete the dependent end of this identifying association. If the current state is Added,
                ' allow the relationship to be changed without causing the dependent to be deleted.
                If previousValue IsNot Nothing AndAlso previousValue.ChangeTracker.State <> ObjectState.Added Then
                    previousValue.MarkAsDeleted()
                End If
            End If
            If Warranty IsNot Nothing AndAlso Not Warranty.ChangeTracker.ChangeTrackingEnabled Then
                Warranty.StartTracking()
            End If
        End If
    End Sub

    Private Sub FixupImages(ByVal sender As Object, ByVal e As NotifyCollectionChangedEventArgs)
        If IsDeserializing Then
            Return
        End If

        If e.NewItems IsNot Nothing Then
            For Each item As Image In e.NewItems
                item.MainAssetsTable = Me
                If ChangeTracker.ChangeTrackingEnabled Then
                    If Not item.ChangeTracker.ChangeTrackingEnabled Then
                        item.StartTracking()
                    End If
                    ChangeTracker.RecordAdditionToCollectionProperties("Images", item)
                End If
            Next
        End If

        If e.OldItems IsNot Nothing Then
            For Each item As Image In e.OldItems
                If item.MainAssetsTable Is Me Then
                    item.MainAssetsTable = Nothing
                End If
                If ChangeTracker.ChangeTrackingEnabled Then
                    ChangeTracker.RecordRemovalFromCollectionProperties("Images", item)
                End If
            Next
        End If
    End Sub

#End Region



    '#Region "Primitive Properties"

    '    Private _RFIDno As String
    '    <DataMember()>
    '    Public Property RFIDno() As String
    '        Get
    '            Return _RFIDno
    '        End Get

    '        Set(ByVal value As String)
    '            If value <> _RFIDno Then
    '                Me.DataStateChanged( _
    '                EntityStateEnum.Modified, _
    '                "RFIDno")
    '            End If
    '            _RFIDno = value
    '            OnPropertyChanged("RFIDno")
    '        End Set
    '    End Property

    '    Private _Assetno As String
    '    <DataMember()>
    '    Public Property Assetno() As String
    '        Get
    '            Return _Assetno
    '        End Get

    '        Set(ByVal value As String)
    '            If Not value Is Nothing Then
    '                If value <> _Assetno Then
    '                    Me.DataStateChanged( _
    '                    EntityStateEnum.Modified, _
    '                    "Assetno")
    '                End If
    '            Else
    '                Throw New Exception("Please provide value for Asset Number.")
    '            End If
    '            _Assetno = value
    '            OnPropertyChanged("Assetno")
    '        End Set
    '    End Property

    '    Private _DateAcquired As Nullable(Of Date)
    '    <DataMember()>
    '    Public Property DateAcquired() As Nullable(Of Date)
    '        Get
    '            Return _DateAcquired
    '        End Get

    '        Set(ByVal value As Nullable(Of Date))
    '            If _DateAcquired Is Nothing Then
    '                _DateAcquired = #12:00:00 AM#
    '            End If
    '            If value <> _DateAcquired Then
    '                Me.DataStateChanged( _
    '                EntityStateEnum.Modified, _
    '                "DateAcquired")
    '            End If
    '            _DateAcquired = value
    '            OnPropertyChanged("DateAcquired")
    '        End Set
    '    End Property

    '    Private _AssetAmount As Nullable(Of Decimal)
    '    <DataMember()>
    '    Public Property AssetAmount As Nullable(Of Decimal)
    '        Get
    '            Return _AssetAmount
    '        End Get

    '        Set(ByVal value As Nullable(Of Decimal))
    '            If value <> _AssetAmount Then
    '                Me.DataStateChanged( _
    '                EntityStateEnum.Modified, _
    '                "AssetAmount")
    '            End If
    '            _AssetAmount = value
    '            OnPropertyChanged("AssetAmount")
    '        End Set
    '    End Property

    '    Private _CategoryCode As String
    '    <DataMember()>
    '    Public Property CategoryCode() As String
    '        Get
    '            Return _CategoryCode
    '        End Get

    '        Set(ByVal value As String)
    '            If Not value Is Nothing Then
    '                If value <> _CategoryCode Then
    '                    Me.DataStateChanged( _
    '                    EntityStateEnum.Modified, _
    '                    "CategoryCode")
    '                End If
    '            Else
    '                Throw New Exception("Please provide value for Category Code.")
    '            End If
    '            _CategoryCode = value
    '            OnPropertyChanged("CategoryCode")
    '        End Set
    '    End Property

    '    Private _LocationCode As String
    '    <DataMember()>
    '    Public Property LocationCode() As String
    '        Get
    '            Return _LocationCode
    '        End Get

    '        Set(ByVal value As String)
    '            If value <> _LocationCode Then
    '                Me.DataStateChanged( _
    '                EntityStateEnum.Modified, _
    '                "LocationCode")
    '            End If
    '            _LocationCode = value
    '            OnPropertyChanged("LocationCode")
    '        End Set
    '    End Property

    '    Private _DeptCode As String
    '    <DataMember()>
    '    Public Property DeptCode() As String
    '        Get
    '            Return _DeptCode
    '        End Get

    '        Set(ByVal value As String)
    '            If value <> _DeptCode Then
    '                Me.DataStateChanged( _
    '                EntityStateEnum.Modified, _
    '                "DeptCode")
    '            End If
    '            _DeptCode = value
    '            OnPropertyChanged("DeptCode")
    '        End Set
    '    End Property

    '    Private _Section As String
    '    <DataMember()>
    '    Public Property Section() As String
    '        Get
    '            Return _Section
    '        End Get

    '        Set(ByVal value As String)
    '            If value <> _Section Then
    '                Me.DataStateChanged( _
    '                EntityStateEnum.Modified, _
    '                "Section")
    '            End If
    '            _Section = value
    '            OnPropertyChanged("Section")
    '        End Set
    '    End Property

    '    Private _AssignToEmployeeCode As String
    '    <DataMember()>
    '    Public Property AssignToEmployeeCode() As String
    '        Get
    '            Return _AssignToEmployeeCode
    '        End Get

    '        Set(ByVal value As String)
    '            If value <> _AssignToEmployeeCode Then
    '                Me.DataStateChanged( _
    '                EntityStateEnum.Modified, _
    '                "AssignToEmployeeCode")
    '            End If
    '            _AssignToEmployeeCode = value
    '            OnPropertyChanged("AssignToEmployeeCode")
    '        End Set
    '    End Property

    '    Private _DescriptiveName As String
    '    <DataMember()>
    '    Public Property DescriptiveName() As String
    '        Get
    '            Return _DescriptiveName
    '        End Get

    '        Set(ByVal value As String)
    '            If Not value Is Nothing Then
    '                If value <> _DescriptiveName Then
    '                    Me.DataStateChanged( _
    '                    EntityStateEnum.Modified, _
    '                    "DescriptiveName")
    '                End If
    '            Else
    '                Throw New Exception("Please provide value for Description.")
    '            End If
    '            _DescriptiveName = value
    '            OnPropertyChanged("DescriptiveName")
    '        End Set
    '    End Property

    '    Private _Createdby As String
    '    <DataMember()>
    '    Public Property Createdby() As String
    '        Get
    '            Return _Createdby
    '        End Get

    '        Set(ByVal value As String)
    '            If value <> _Createdby Then
    '                Me.DataStateChanged( _
    '                EntityStateEnum.Modified, _
    '                "Createdby")
    '            End If
    '            _Createdby = value
    '            OnPropertyChanged("Createdby")
    '        End Set
    '    End Property

    '    Private _DateCreated As Nullable(Of Date)
    '    <DataMember()>
    '    Public Property DateCreated() As Nullable(Of Date)
    '        Get
    '            Return _DateCreated
    '        End Get

    '        Set(ByVal value As Nullable(Of Date))
    '            If _DateCreated Is Nothing Then
    '                _DateCreated = #12:00:00 AM#
    '            End If
    '            If value <> _DateCreated Then
    '                Me.DataStateChanged( _
    '                EntityStateEnum.Modified, _
    '                "DateCreated")
    '            End If
    '            _DateCreated = value
    '            OnPropertyChanged("DateCreated")
    '        End Set
    '    End Property

    '    Private _LastModified As Nullable(Of Date)
    '    <DataMember()>
    '    Public Property LastModified() As Nullable(Of Date)
    '        Get
    '            Return _LastModified
    '        End Get

    '        Set(ByVal value As Nullable(Of Date))
    '            If _LastModified Is Nothing Then
    '                _LastModified = #12:00:00 AM#
    '            End If
    '            If value <> _LastModified Then
    '                Me.DataStateChanged( _
    '                EntityStateEnum.Modified, _
    '                "LastModified")
    '            End If
    '            _LastModified = value
    '            OnPropertyChanged("LastModified")
    '        End Set
    '    End Property

    '    Private _LastModifiedBy As String
    '    <DataMember()>
    '    Public Property LastModifiedBy() As String
    '        Get
    '            Return _LastModifiedBy
    '        End Get

    '        Set(ByVal value As String)
    '            If value <> _LastModifiedBy Then
    '                Me.DataStateChanged( _
    '                EntityStateEnum.Modified, _
    '                "LastModifiedBy")
    '            End If
    '            _LastModifiedBy = value
    '            OnPropertyChanged("LastModifiedBy")
    '        End Set
    '    End Property

    '    Private _EstimatedValue As Nullable(Of Decimal)
    '    <DataMember()>
    '    Public Property EstimatedValue As Nullable(Of Decimal)
    '        Get
    '            Return _EstimatedValue
    '        End Get

    '        Set(ByVal value As Nullable(Of Decimal))
    '            If value <> _EstimatedValue Then
    '                Me.DataStateChanged( _
    '                EntityStateEnum.Modified, _
    '                "EstimatedValue")
    '            End If
    '            _EstimatedValue = value
    '            OnPropertyChanged("EstimatedValue")
    '        End Set
    '    End Property

    '    Private _ReplacementCost As Nullable(Of Decimal)
    '    <DataMember()>
    '    Public Property ReplacementCost As Nullable(Of Decimal)
    '        Get
    '            Return _ReplacementCost
    '        End Get

    '        Set(ByVal value As Nullable(Of Decimal))
    '            If value <> _ReplacementCost Then
    '                Me.DataStateChanged( _
    '                EntityStateEnum.Modified, _
    '                "ReplacementCost")
    '            End If
    '            _ReplacementCost = value
    '            OnPropertyChanged("ReplacementCost")
    '        End Set
    '    End Property

    '    Private _Condition As String
    '    <DataMember()>
    '    Public Property Condition() As String
    '        Get
    '            Return _Condition
    '        End Get

    '        Set(ByVal value As String)
    '            If value <> _Condition Then
    '                Me.DataStateChanged( _
    '                EntityStateEnum.Modified, _
    '                "Condition")
    '            End If
    '            _Condition = value
    '            OnPropertyChanged("Condition")
    '        End Set
    '    End Property

    '    Private _Status As String
    '    <DataMember()>
    '    Public Property Status() As String
    '        Get
    '            Return _Status
    '        End Get

    '        Set(ByVal value As String)
    '            If value <> _Status Then
    '                Me.DataStateChanged( _
    '                EntityStateEnum.Modified, _
    '                "Status")
    '            End If
    '            _Status = value
    '            OnPropertyChanged("Status")
    '        End Set
    '    End Property

    '    Private _SerialNumber As String
    '    <DataMember()>
    '    Public Property SerialNumber() As String
    '        Get
    '            Return _SerialNumber
    '        End Get

    '        Set(ByVal value As String)
    '            If value <> _SerialNumber Then
    '                Me.DataStateChanged( _
    '                EntityStateEnum.Modified, _
    '                "SerialNumber")
    '            End If
    '            _SerialNumber = value
    '            OnPropertyChanged("SerialNumber")
    '        End Set
    '    End Property

    '    Private _InvoiceNumber As String
    '    <DataMember()>
    '    Public Property InvoiceNumber() As String
    '        Get
    '            Return _InvoiceNumber
    '        End Get

    '        Set(ByVal value As String)
    '            If value <> _InvoiceNumber Then
    '                Me.DataStateChanged( _
    '                EntityStateEnum.Modified, _
    '                "InvoiceNumber")
    '            End If
    '            _InvoiceNumber = value
    '            OnPropertyChanged("InvoiceNumber")
    '        End Set
    '    End Property

    '    Private _BoughtFrom As String
    '    <DataMember()>
    '    Public Property BoughtFrom() As String
    '        Get
    '            Return _BoughtFrom
    '        End Get

    '        Set(ByVal value As String)
    '            If value <> _BoughtFrom Then
    '                Me.DataStateChanged( _
    '                EntityStateEnum.Modified, _
    '                "BoughtFrom")
    '            End If
    '            _BoughtFrom = value
    '            OnPropertyChanged("BoughtFrom")
    '        End Set
    '    End Property

    '    Private _BaseCost As Nullable(Of Decimal)
    '    <DataMember()>
    '    Public Property BaseCost As Nullable(Of Decimal)
    '        Get
    '            Return _BaseCost
    '        End Get

    '        Set(ByVal value As Nullable(Of Decimal))
    '            If value <> _BaseCost Then
    '                Me.DataStateChanged( _
    '                EntityStateEnum.Modified, _
    '                "BaseCost")
    '            End If
    '            _BaseCost = value
    '            OnPropertyChanged("BaseCost")
    '            OnPropertyChanged("TotalCost")
    '        End Set
    '    End Property

    '    Private _VAT As Nullable(Of Decimal)
    '    <DataMember()>
    '    Public Property VAT As Nullable(Of Decimal)
    '        Get
    '            Return _VAT
    '        End Get

    '        Set(ByVal value As Nullable(Of Decimal))
    '            If value <> _VAT Then
    '                Me.DataStateChanged( _
    '                EntityStateEnum.Modified, _
    '                "VAT")
    '            End If
    '            _VAT = value
    '            OnPropertyChanged("VAT")
    '            OnPropertyChanged("TotalCost")
    '        End Set
    '    End Property

    '    Private _Manufacturer As String
    '    <DataMember()>
    '    Public Property Manufacturer() As String
    '        Get
    '            Return _Manufacturer
    '        End Get

    '        Set(ByVal value As String)
    '            If value <> _Manufacturer Then
    '                Me.DataStateChanged( _
    '                EntityStateEnum.Modified, _
    '                "Manufacturer")
    '            End If
    '            _Manufacturer = value
    '            OnPropertyChanged("Manufacturer")
    '        End Set
    '    End Property

    '    Private _BrandName As String
    '    <DataMember()>
    '    Public Property BrandName() As String
    '        Get
    '            Return _BrandName
    '        End Get

    '        Set(ByVal value As String)
    '            If value <> _BrandName Then
    '                Me.DataStateChanged( _
    '                EntityStateEnum.Modified, _
    '                "BrandName")
    '            End If
    '            _BrandName = value
    '            OnPropertyChanged("BrandName")
    '        End Set
    '    End Property

    '    Private _ModelNumber As String
    '    <DataMember()>
    '    Public Property ModelNumber() As String
    '        Get
    '            Return _ModelNumber
    '        End Get

    '        Set(ByVal value As String)
    '            If value <> _ModelNumber Then
    '                Me.DataStateChanged( _
    '                EntityStateEnum.Modified, _
    '                "ModelNumber")
    '            End If
    '            _ModelNumber = value
    '            OnPropertyChanged("ModelNumber")
    '        End Set
    '    End Property

    '    Private _Remarks As String
    '    <DataMember()>
    '    Public Property Remarks() As String
    '        Get
    '            Return _Remarks
    '        End Get

    '        Set(ByVal value As String)
    '            If value <> _Remarks Then
    '                Me.DataStateChanged( _
    '                EntityStateEnum.Modified, _
    '                "Remarks")
    '            End If
    '            _Remarks = value
    '            OnPropertyChanged("Remarks")
    '        End Set
    '    End Property

    '    Private _SalvageValue As Nullable(Of Decimal)
    '    <DataMember()>
    '    Public Property SalvageValue As Nullable(Of Decimal)
    '        Get
    '            Return _SalvageValue
    '        End Get

    '        Set(ByVal value As Nullable(Of Decimal))
    '            If value <> _SalvageValue Then
    '                Me.DataStateChanged( _
    '                EntityStateEnum.Modified, _
    '                "SalvageValue")
    '            End If
    '            _SalvageValue = value
    '            OnPropertyChanged("SalvageValue")
    '        End Set
    '    End Property

    '    Private _LastScanDate As Nullable(Of Date)
    '    <DataMember()>
    '    Public Property LastScanDate() As Nullable(Of Date)
    '        Get
    '            Return _LastScanDate
    '        End Get

    '        Set(ByVal value As Nullable(Of Date))
    '            If _LastScanDate Is Nothing Then
    '                _LastScanDate = #12:00:00 AM#
    '            End If
    '            If value <> _LastScanDate Then
    '                Me.DataStateChanged( _
    '                EntityStateEnum.Modified, _
    '                "LastScanDate")
    '            End If
    '            _LastScanDate = value
    '            OnPropertyChanged("LastScanDate")
    '        End Set
    '    End Property

    '    Public ReadOnly Property IsDirty() As Boolean
    '        Get
    '            Return Me.EntityState <> EntityStateEnum.Unchanged
    '        End Get
    '    End Property

    '    Private _TotalCost As Nullable(Of Decimal)
    '    Public ReadOnly Property TotalCost() As Nullable(Of Decimal)
    '        Get
    '            Return BaseCost + VAT
    '        End Get

    '        'Set(value As Nullable(Of Decimal))
    '        '    _TotalCost = value
    '        'End Set
    '    End Property
    '#End Region
    '#Region "Navigation Properties"
    '    <DataMember()>
    '    Public Overridable Property Category As Category
    '        Get
    '            Return _category
    '        End Get
    '        Set(ByVal value As Category)
    '            If _category IsNot value Then
    '                Dim previousValue As Category = _category
    '                _category = value
    '                FixupCategory(previousValue)
    '            End If
    '        End Set
    '    End Property
    '    Private _category As Category

    '    <DataMember()>
    '    Public Overridable Property Department As Department
    '        Get
    '            Return _department
    '        End Get
    '        Set(ByVal value As Department)
    '            If _department IsNot value Then
    '                Dim previousValue As Department = _department
    '                _department = value
    '                FixupDepartment(previousValue)
    '            End If
    '        End Set
    '    End Property
    '    Private _department As Department

    '    <DataMember()>
    '    Public Overridable Property DisposedAsset As DisposedAsset
    '        Get
    '            Return _disposedAsset
    '        End Get
    '        Set(ByVal value As DisposedAsset)
    '            If _disposedAsset IsNot value Then
    '                Me.DataStateChanged( _
    '               EntityStateEnum.Modified, _
    '               "DisposedAsset")

    '                Dim previousValue As DisposedAsset = _disposedAsset
    '                _disposedAsset = value
    '                FixupDisposedAsset(previousValue)
    '            End If

    '            OnPropertyChanged("DisposedAsset")
    '        End Set
    '    End Property
    '    Private _disposedAsset As DisposedAsset

    '    <DataMember()>
    '    Public Overridable Property Images As ICollection(Of Image)
    '        Get
    '            If _images Is Nothing Then
    '                Dim newCollection As New FixupCollection(Of Image)
    '                AddHandler newCollection.CollectionChanged, AddressOf FixupImages
    '                _images = newCollection
    '            End If
    '            Return _images
    '        End Get
    '        Set(ByVal value As ICollection(Of Image))
    '            If _images IsNot value Then
    '                Dim previousValue As FixupCollection(Of Image) = TryCast(_images, FixupCollection(Of Image))
    '                If previousValue IsNot Nothing Then
    '                    RemoveHandler previousValue.CollectionChanged, AddressOf FixupImages
    '                End If
    '                _images = value
    '                Dim newValue As FixupCollection(Of Image) = TryCast(value, FixupCollection(Of Image))
    '                If newValue IsNot Nothing Then
    '                    AddHandler newValue.CollectionChanged, AddressOf FixupImages
    '                End If
    '            End If
    '        End Set
    '    End Property
    '    Private _images As ICollection(Of Image)

    '    <DataMember()>
    '    Public Overridable Property LeasedAsset As LeasedAsset
    '        Get
    '            Return _leasedAsset
    '        End Get
    '        Set(ByVal value As LeasedAsset)
    '            If _leasedAsset IsNot value Then
    '                Dim previousValue As LeasedAsset = _leasedAsset
    '                _leasedAsset = value
    '                FixupLeasedAsset(previousValue)
    '            End If
    '        End Set
    '    End Property
    '    Private _leasedAsset As LeasedAsset

    '    <DataMember()>
    '    Public Overridable Property Location As Location
    '        Get
    '            Return _location
    '        End Get
    '        Set(ByVal value As Location)
    '            If _location IsNot value Then
    '                Dim previousValue As Location = _location
    '                _location = value
    '                FixupLocation(previousValue)
    '            End If
    '        End Set
    '    End Property
    '    Private _location As Location

    '    <DataMember()>
    '    Public Overridable Property Person As Person
    '        Get
    '            Return _person
    '        End Get
    '        Set(ByVal value As Person)
    '            If _person IsNot value Then
    '                Dim previousValue As Person = _person
    '                _person = value
    '                FixupPerson(previousValue)
    '            End If
    '        End Set
    '    End Property
    '    Private _person As Person

    '    <DataMember()>
    '    Public Overridable Property Warranty As Warranty
    '        Get
    '            Return _warranty
    '        End Get
    '        Set(ByVal value As Warranty)
    '            If _warranty IsNot value Then
    '                Dim previousValue As Warranty = _warranty
    '                _warranty = value
    '                FixupWarranty(previousValue)
    '            End If
    '        End Set
    '    End Property
    '    Private _warranty As Warranty

    '#End Region
    '#Region "Association Fixup"
    '    Private _settingFK As Boolean = False

    '    Private Sub FixupCategory(ByVal previousValue As Category)
    '        If previousValue IsNot Nothing AndAlso previousValue.MainAssetsTables.Contains(Me) Then
    '            previousValue.MainAssetsTables.Remove(Me)
    '        End If
    '        If Category IsNot Nothing Then
    '            If Not Category.MainAssetsTables.Contains(Me) Then
    '                Category.MainAssetsTables.Add(Me)
    '            End If
    '            If Not Equals(CategoryCode, Category.CategoryCode) Then
    '                CategoryCode = Category.CategoryCode
    '            End If
    '        ElseIf Not _settingFK Then
    '            CategoryCode = Nothing
    '        End If
    '    End Sub

    '    Private Sub FixupDepartment(ByVal previousValue As Department)
    '        If previousValue IsNot Nothing AndAlso previousValue.MainAssetsTables.Contains(Me) Then
    '            previousValue.MainAssetsTables.Remove(Me)
    '        End If
    '        If Department IsNot Nothing Then
    '            If Not Department.MainAssetsTables.Contains(Me) Then
    '                Department.MainAssetsTables.Add(Me)
    '            End If
    '            If Not Equals(DeptCode, Department.DepartmentCode) Then
    '                DeptCode = Department.DepartmentCode
    '            End If
    '        ElseIf Not _settingFK Then
    '            DeptCode = Nothing
    '        End If
    '    End Sub

    '    Private Sub FixupDisposedAsset(ByVal previousValue As DisposedAsset)
    '        If previousValue IsNot Nothing AndAlso previousValue.MainAssetsTable Is Me Then
    '            previousValue.MainAssetsTable = Nothing
    '        End If
    '        If DisposedAsset IsNot Nothing Then
    '            DisposedAsset.MainAssetsTable = Me
    '        End If
    '    End Sub

    '    Private Sub FixupLeasedAsset(ByVal previousValue As LeasedAsset)
    '        If previousValue IsNot Nothing AndAlso previousValue.MainAssetsTable Is Me Then
    '            previousValue.MainAssetsTable = Nothing
    '        End If
    '        If LeasedAsset IsNot Nothing Then
    '            LeasedAsset.MainAssetsTable = Me
    '        End If
    '    End Sub

    '    Private Sub FixupLocation(ByVal previousValue As Location)
    '        If previousValue IsNot Nothing AndAlso previousValue.MainAssetsTables.Contains(Me) Then
    '            previousValue.MainAssetsTables.Remove(Me)
    '        End If
    '        If Location IsNot Nothing Then
    '            If Not Location.MainAssetsTables.Contains(Me) Then
    '                Location.MainAssetsTables.Add(Me)
    '            End If
    '            If Not Equals(LocationCode, Location.LocationCode) Then
    '                LocationCode = Location.LocationCode
    '            End If
    '        ElseIf Not _settingFK Then
    '            LocationCode = Nothing
    '        End If
    '    End Sub

    '    Private Sub FixupPerson(ByVal previousValue As Person)
    '        If previousValue IsNot Nothing AndAlso previousValue.MainAssetsTables.Contains(Me) Then
    '            previousValue.MainAssetsTables.Remove(Me)
    '        End If
    '        If Person IsNot Nothing Then
    '            If Not Person.MainAssetsTables.Contains(Me) Then
    '                Person.MainAssetsTables.Add(Me)
    '            End If
    '            If Not Equals(AssignToEmployeeCode, Person.EmployeeID) Then
    '                AssignToEmployeeCode = Person.EmployeeID
    '            End If
    '        ElseIf Not _settingFK Then
    '            AssignToEmployeeCode = Nothing
    '        End If
    '    End Sub

    '    Private Sub FixupWarranty(ByVal previousValue As Warranty)
    '        If previousValue IsNot Nothing AndAlso previousValue.MainAssetsTable Is Me Then
    '            previousValue.MainAssetsTable = Nothing
    '        End If
    '        If Warranty IsNot Nothing Then
    '            Warranty.MainAssetsTable = Me
    '        End If
    '    End Sub
    '    Private Sub FixupImages(ByVal sender As Object, ByVal e As NotifyCollectionChangedEventArgs)
    '        If e.NewItems IsNot Nothing Then
    '            For Each item As Image In e.NewItems
    '                item.MainAssetsTable = Me
    '            Next
    '        End If
    '        If e.OldItems IsNot Nothing Then
    '            For Each item As Image In e.OldItems
    '                If item.MainAssetsTable Is Me Then
    '                    item.MainAssetsTable = Nothing
    '                End If
    '            Next
    '        End If
    '    End Sub

    '#End Region

#Region "Support Procedures"
    Public Sub ResetDirtyBit()
        Me.EntityState = EntityStateEnum.Unchanged
    End Sub

    Public Sub BeginEdit() Implements System.ComponentModel.IEditableObject.BeginEdit
        mainAssetData = New MainAssetsTable
        With mainAssetData
            .RFIDno = RFIDno
            .Assetno = Assetno
            .DateAcquired = DateAcquired
            .AssetAmount = AssetAmount
            .CategoryCode = CategoryCode
            .LocationCode = LocationCode
            .DeptCode = DeptCode
            .Section = Section
            .AssignToEmployeeCode = AssignToEmployeeCode
            .DescriptiveName = DescriptiveName
            .Createdby = Createdby
            .DateCreated = DateCreated
            .LastModified = LastModified
            .LastModifiedBy = LastModifiedBy
            .EstimatedValue = EstimatedValue
            .ReplacementCost = ReplacementCost
            .Condition = Condition
            .Status = Status
            .SerialNumber = SerialNumber
            .InvoiceNumber = InvoiceNumber
            .BoughtFrom = BoughtFrom
            .BaseCost = BaseCost
            .VAT = VAT
            .Manufacturer = Manufacturer
            .BrandName = BrandName
            .ModelNumber = ModelNumber
            .Remarks = Remarks
            .SalvageValue = SalvageValue
            .LastScanDate = LastScanDate
            If Not DisposedAsset Is Nothing Then
                DisposedAsset.BeginEdit()
            End If
        End With
        
    End Sub

    Public Sub CancelEdit() Implements System.ComponentModel.IEditableObject.CancelEdit
        If Not mainAssetData Is Nothing Then
            With mainAssetData
                RFIDno = .RFIDno
                Assetno = .Assetno
                DateAcquired = .DateAcquired
                AssetAmount = .AssetAmount
                CategoryCode = .CategoryCode
                LocationCode = .LocationCode
                DeptCode = .DeptCode
                Section = .Section
                AssignToEmployeeCode = .AssignToEmployeeCode
                DescriptiveName = .DescriptiveName
                Createdby = .Createdby
                DateCreated = .DateCreated
                LastModified = .LastModified
                LastModifiedBy = .LastModifiedBy
                EstimatedValue = .EstimatedValue
                ReplacementCost = .ReplacementCost
                Condition = .Condition
                Status = .Status
                SerialNumber = .SerialNumber
                InvoiceNumber = .InvoiceNumber
                BoughtFrom = .BoughtFrom
                BaseCost = .BaseCost
                VAT = .VAT
                Manufacturer = .Manufacturer
                BrandName = .BrandName
                ModelNumber = .ModelNumber
                Remarks = .Remarks
                SalvageValue = .SalvageValue
                LastScanDate = .LastScanDate
                If Not .DisposedAsset Is Nothing Then
                    DisposedAsset.CancelEdit()
                End If
            End With

        End If

    End Sub

    Public Sub EndEdit() Implements System.ComponentModel.IEditableObject.EndEdit
        mainAssetData = Nothing
    End Sub

    'Public Event PropertyChanged(sender As Object, e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged

    'Public Sub OnPropertyChanged(ByVal propertyName As String)
    '    RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
    'End Sub

    Private _EntityState As EntityStateEnum
    Public Property EntityState() As EntityStateEnum
        Get
            Return _EntityState
        End Get
        Set(ByVal value As EntityStateEnum)
            _EntityState = value
        End Set
    End Property

    Private Sub DataStateChanged(ByVal dataState As EntityStateEnum, ByVal propertyName As String)
        ' Raise the event
        If Not String.IsNullOrEmpty(propertyName) Then
            RaiseEvent PropertyChanged(Me, _
            New PropertyChangedEventArgs(propertyName))
        End If

        ' If the state is deleted, mark it as deleted
        If dataState = EntityStateEnum.Deleted Then
            Me.EntityState = dataState
        End If

        If Me.EntityState = _
                       EntityStateEnum.Unchanged Then
            Me.EntityState = dataState
        End If
    End Sub



#End Region
End Class





